package com.onetech.adrewards.free.adapters;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.onetech.adrewards.free.objects.ExchangeDTO;
import com.onetech.adrewards.free.R;


public class ExchangeListAdapter extends BaseAdapter {
	private class ExchangeViewHolder {
		public TextView giftCardCode;
		public TextView giftcardprice; 
		public TextView createdAt;
		public TextView expireDate;
		public ImageView cover;
//		public TextView type;
//		public TextView totalCoinExchanged;
	}

	private Activity activity;
	private List<ExchangeDTO> listApps;
	private static LayoutInflater inflater = null;

	public ExchangeListAdapter(Activity _activity) {
		activity = _activity;
		listApps = new ArrayList<ExchangeDTO>();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateAppList(List<ExchangeDTO> _listAppDTOs) {
		listApps.clear();
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	public void insertAppList(List<ExchangeDTO> _listAppDTOs) {
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listApps.size();
	}

	@Override
	public Object getItem(int arg0) {
		if (listApps == null) {
			return null;
		}
		return listApps.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ExchangeViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.exchange_item_row_listview,parent, false);
			viewHolder = new ExchangeViewHolder();
			viewHolder.giftCardCode = (TextView) convertView
					.findViewById(R.id.gifCardCode);	
			viewHolder.createdAt = (TextView) convertView
					.findViewById(R.id.giftCardDate);
			viewHolder.giftcardprice = (TextView) convertView
					.findViewById(R.id.giftCardPrice);
			viewHolder.expireDate = (TextView) convertView
					.findViewById(R.id.giftCardExpireDate);
			
			viewHolder.cover = (ImageView) convertView
					.findViewById(R.id.giftCardCover);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ExchangeViewHolder) convertView.getTag();
		}
		ExchangeDTO temp = listApps.get(position);
		viewHolder.giftCardCode.setText(temp.getGift_card_code().trim());
		viewHolder.createdAt.setText("発行日時：" + temp.getCreated_at());
		viewHolder.giftcardprice.setText(Html.fromHtml(temp.getUseCoin() + "pt→<font color=\"#FF8300\"><b>" + temp.getPrice() + "円</b></font>"));
		if(!TextUtils.isEmpty(temp.getExpireDate()))
		{
			viewHolder.expireDate.setVisibility(View.VISIBLE);
			viewHolder.expireDate.setText(temp.getExpireDate());
		}
		else
		{
			viewHolder.expireDate.setVisibility(View.GONE);
		}
		
		//viewHolder.totalCoinExchanged.setText(temp.getUse_coin());
		ImageLoader.getInstance(activity.getApplicationContext()).DisplayImage(
				temp.getImg(), viewHolder.cover);

		return convertView;
	}

}

